//
//  AppDelegate.m
//  CallTelphone
//
//  Created by QFX on 2023/5/22.
//

#import "AppDelegate.h"
#import "UIImage+ChangeColor.h"
#import "CallListViewController.h"
#import "CallTelphoneViewController.h"
//#import <Bugly/Bugly.h>
#import "JPUSHService.h"
#import <UserNotifications/UserNotifications.h>
#import "JPush.h"

@interface AppDelegate ()<JPUSHRegisterDelegate>

@property (nonatomic,strong) UITabBarController *tabBarController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
//    [Bugly startWithAppId:@"c141026f41"];
          
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.tabBarController = [[UITabBarController alloc] init];
    _tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
    _tabBarController.tabBar.barTintColor = [UIColor whiteColor];

    
    UIViewController *v1 = [[UIViewController alloc] init];
    v1.view.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:v1];
    
    CallListViewController *v2 = [[CallListViewController alloc] init];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:v2];

    UIViewController *v3 = [[UIViewController alloc] init];
    v3.view.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:v3];

    CallTelphoneViewController *v4 = [[CallTelphoneViewController alloc] initWithNibName:@"CallTelphoneViewController" bundle:nil];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:v4];

    UIViewController *v5 = [[UIViewController alloc] init];
    v5.view.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:v5];
    
    UIImage *icon1 = [UIImage imageNamed:@"icon1.png"];
    UIImage *icon1_sel = [UIImage imageNamed:@"icon1_sel.png"];

    UIImage *icon2 = [UIImage imageNamed:@"icon2.png"];
    UIImage *icon2_sel = [UIImage imageNamed:@"icon2_sel.png"];

    UIImage *icon3 = [UIImage imageNamed:@"icon3.png"];
    UIImage *icon3_sel = [UIImage imageNamed:@"icon3_sel.png"];

    UIImage *icon4 = [UIImage imageNamed:@"icon4.png"];
    UIImage *icon4_sel = [UIImage imageNamed:@"icon4_sel.png"];

    UIImage *icon5 = [UIImage imageNamed:@"icon5.png"];
    UIImage *icon5_sel = [UIImage imageNamed:@"icon5_sel.png"];


    nav1.tabBarItem.title = @"个人收藏";
    nav1.tabBarItem.image = [icon1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav1.tabBarItem.selectedImage = [icon1_sel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav2.tabBarItem.title = @"最近通话";
    nav2.tabBarItem.image = [icon2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav2.tabBarItem.selectedImage = [icon2_sel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav3.tabBarItem.title = @"通讯录";
    nav3.tabBarItem.image = [icon3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav3.tabBarItem.selectedImage = [icon3_sel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav4.tabBarItem.title = @"拨号键盘";
    nav4.tabBarItem.image = [icon4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav4.tabBarItem.selectedImage = [icon4_sel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    nav5.tabBarItem.title = @"语音留言";
    nav5.tabBarItem.image = [icon5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    nav5.tabBarItem.selectedImage = [icon5_sel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.tabBarController.viewControllers = @[nav1,nav2,nav3,nav4,nav5];
    self.tabBarController.selectedIndex = 1;
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    
    
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

    //【初始化sdk】
    [JPUSHService setupWithOption:launchOptions
                           appKey:@"49d9d65766c7bff6c28d1d29"
                          channel:@""
                 apsForProduction:NO
            advertisingIdentifier:nil];
    
    [JPUSHService setAlias:@"121212" completion:^(NSInteger iResCode, NSString * _Nullable iAlias, NSInteger seq) {
        
    } seq:0];
    
    
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    //sdk注册DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
    //在UI上展示
    [[JPush shared] deviceToken: deviceToken];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
  [application setApplicationIconBadgeNumber:0];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
  [application setApplicationIconBadgeNumber:0];
}

- (void)application:(UIApplication *)application
    didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}


#pragma mark- JPUSHRegisterDelegate
//设置消息送达代理
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler {
    NSDictionary *userInfo = notification.request.content.userInfo;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Incoming" object:nil userInfo:userInfo];

    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以设置
    
    [[JPush shared] willPresentNotification:notification];
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
  
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Incoming" object:nil userInfo:userInfo];

    
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler();  // 系统要求执行这个方法
    
    [[JPush shared] didReceiveNotificationResponse:response];
}

- (void)jpushNotificationAuthorization:(JPAuthorizationStatus)status withInfo:(NSDictionary *)info {
  NSLog(@"receive notification authorization status:%lu, info:%@", status, info);
  [[JPush shared] alertNotificationAuthorization:status];
}



- (UIImage *)tintImageWithColor:(UIColor *)tintColor image:(UIImage *)image{
    UIGraphicsBeginImageContextWithOptions(image.size , NO ,1);
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    CGContextTranslateCTM (context, 0 , image.size.height );
    CGContextScaleCTM (context, 1.0 , - 1.0 );
   
    CGRect rect = CGRectMake ( 0 , 0 , image.size.width , image.size.height );
   
    CGContextSetBlendMode (context, kCGBlendModeNormal );
    CGContextDrawImage (context, rect, image.CGImage );
   
    CGContextSetBlendMode (context, kCGBlendModeSourceIn );
    [tintColor setFill ];
    CGContextFillRect (context, rect);
   
    UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext ();
    UIGraphicsEndImageContext ();
    return coloredImage;
}
@end
